package com.example.algorithm.greedy;

/**
 * @author : zhy
 * @since : 2022/8/26
 * 最大子数组和
 */
public class MaxSumArraySon {

    public int maxSubArray(int[] nums) {
        int len=nums.length;
        if(len==1)
        {
            return nums[0];
        }
        int sum=Integer.MIN_VALUE;
        int count=0;

        for(int i=0;i<len;i++)
        {
            count+=nums[i];
            sum=Math.max(sum,count);
            if(count<=0)
            {
                count=0;
            }
        }
        return sum;
    }

    public static void main(String[] args) {
        MaxSumArraySon maxSumArraySon = new MaxSumArraySon();
        int []can={-2,1,-3,4,-1,2,1,-5,4};
        int i = maxSumArraySon.maxSubArray(can);
        System.out.println(i);
    }
}
